<template>
	<view class="login-container">
		<!-- 顶部品牌logo -->
		<view class="brand-container">
			<image class="brand-logo" src="/static/logo.jpg" mode="aspectFit"></image>
			<view class="brand-name">威佳山哥说车</view>
		</view>

		<!-- 整备记录 -->
		<view class="member-benefit">
			<text>威佳山哥说车，整备记录</text>
		</view>

		<!-- 登录表单 -->
		<view class="login-form">
			<view class="login-tip">
				<p>授权绑定手机号 为您提供更好的服务</p>
				<p>账号仅限特定人群登录并进行登录账号鉴权</p>
			</view>

			<!-- 一键登录按钮 -->
			<button class="quick-login-btn" @click="generalLogin" :loading="loginLoading">
				一键登录
			</button>
			<button class="quick-login-btn" open-type="getPhoneNumber" @getphonenumber="handlePhoneLogin">
				手机号一键登录
			</button>

			<!-- 用户协议 -->
			<!-- <view class="agreement">
        <checkbox-group @change="toggleAgree">
          <label class="agreement-label">
            <checkbox :checked="isAgree" color="#FF2C3C"/>
            <text>已阅读并同意</text>
          </label>
        </checkbox-group>
        <text class="agreement-link" @click="navigateToAgreement">《用户服务协议》</text>
        <text class="agreement-link" @click="navigateToPrivacy">《个人信息保护政策》</text>
      </view> -->

			<!-- 暂不登录 -->
			<!--      <view class="skip-login" @click="skipLogin">
        <text>暂不登录</text>
      </view> -->
		</view>
	</view>
</template>

<script>
import {
	phoneLogin,
	login
} from '@/api/app.js'
export default {
	data() {
		return {
			isAgree: false,
			loginLoading: false
		}
	},
	onShow() {
		const isLogin = uni.getStorageSync('isLogin')
		console.log(isLogin, 'isLogin')

		if (isLogin) {
			uni.switchTab({
				url: '/pages/home/index'
			})
		}
	},
	methods: {
		// 普通登录
		async generalLogin() {
			uni.showLoading({
				title: '登录中...'
			})
			const wxres = await this.getWxCode()
			const res = await login({
				code: wxres.code
			})
			if (res.code === 1) {
				const user = res.data[0]
				this.setUserInfo(user)

				uni.hideLoading()
			} else {
				uni.showToast({
					icon: 'none',
					title: '登录失败,' + res.message
				})
			}
		},

		// 微信获取code
		getWxCode() {
			return new Promise((resolve, reject) => {
				uni.login({
					success: (res) => {
						if (res.code) {
							resolve(res)
						} else {
							reject(new Error('获取微信code失败'))
						}
					},
					fail: (err) => {
						reject(err)
					}
				})
			})
		},

		// 设置缓存
		setUserInfo(user) {
			uni.setStorageSync('name', user.name)
			uni.setStorageSync('id', user.id)
			uni.setStorageSync('mobile', user.mobile)
			uni.setStorageSync('qx', user.memberAuthId)
			uni.setStorageSync('isLogin', true)

			uni.showToast({
				icon: 'none',
				title: '登录成功'
			})

			uni.switchTab({
				url: '/pages/home/index'
			})
		},

		// 微信授权登录
		async handlePhoneLogin(e) {
			console.log(e)
			if (e.detail.errMsg.includes('user deny')) {
				uni.showToast({
					icon: 'none',
					title: '用户拒绝授权'
				})
				return
			}

			if (e.detail.errMsg === 'getPhoneNumber:ok') {
				uni.showLoading({
					title: '登录中...'
				})

				// 这里应该将 e.detail 中的加密数据发送到后端解密获取手机号

				const wxres = await this.getWxCode()

				const res = await phoneLogin({
					code: wxres.code,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv
				})

				if (res.code === 1) {
					const user = res.data[0]
					uni.hideLoading()
					this.setUserInfo(user)
				} else {
					uni.showToast({
						icon: 'none',
						title: '登录失败,' + res.message
					})
				}
			} else {
				uni.hideLoading()

				uni.showToast({
					title: '授权失败',
					icon: 'none'
				})
			}
		},

		// 同意协议
		toggleAgree(e) {
			this.isAgree = e.detail.value.length > 0
		},

		// 查看用户协议
		navigateToAgreement() {
			uni.navigateTo({
				url: '/pages/agreement/user'
			})
		},

		// 查看隐私政策
		navigateToPrivacy() {
			uni.navigateTo({
				url: '/pages/agreement/privacy'
			})
		},

		// 暂不登录
		skipLogin() {
			uni.switchTab({
				url: '/pages/home/home'
			})
		}
	}
}
</script>

<style lang="scss">
.login-container {
	min-height: 100vh;
	padding: 40rpx;
	background-color: #fff;
	display: flex;
	flex-direction: column;
	align-items: center;
	box-sizing: border-box;

	.brand-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 80rpx;
		margin-bottom: 60rpx;

		.brand-logo {
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.brand-name {
			font-size: 28rpx;
			color: #000000;
		}
	}

	.member-benefit {
		font-size: 40rpx;
		font-weight: bold;
		color: #000000;
		margin: 80rpx 0;
		text-align: center;
	}

	.login-form {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;

		.login-tip {
			font-size: 24rpx;
			color: #666;
			margin-bottom: 20rpx;
			text-align: center;
		}

		.quick-login-btn {
			width: 80%;
			height: 80rpx;
			line-height: 80rpx;
			background-color: #409eff;
			color: #fff;
			font-size: 26rpx;
			border-radius: 10rpx;
			margin-bottom: 30rpx;
		}

		.agreement {
			display: flex;
			flex-wrap: wrap;
			justify-content: center;
			align-items: center;
			font-size: 24rpx;
			color: #999;
			margin-bottom: 60rpx;
			width: 80%;

			.agreement-label {
				display: flex;
				align-items: center;
				margin-right: 4rpx;
			}

			.agreement-link {
				color: #409eff;
				margin-right: 4rpx;
			}
		}

		.skip-login {
			font-size: 28rpx;
			color: #999;
			text-decoration: underline;
		}
	}
}
</style>